<template>
  <div>
   
    <hr />
    <el-form :inline="true" ref="form" :model="form" class="demo-form-inline" >
      <el-form-item label="专业名称">
        <el-input v-model="form.name" placeholder="请输入"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="addSubject">新增专业</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="subjects"
      border
      style="width: 100%; margin-top: 20px; margin-bottom: 20px"
    >
      <el-table-column prop="_id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="专业名称"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageData.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      subjects: [],
      form: {
        name: "",
        },
      pageData: {
        currentPage: 1,
        pageSize: 5,
      },
      total: 0,
      
    };
  },
  created() {
    this.getSubjects();
    this.getSubjects();
  },
  //侦听页码变化
  watch: {
    pageData: {
      handler() {
        this.getSubjects();
      },
      deep: true,
    },
  },
  methods: {
    //搜索
    searchStudents() {
      // console.log(this.searchData);
      this.getStudents();
    },
    //获取全部专业数据并渲染
    async getSubjects() {
      const res = await this.$api.subjects.get(this.pageData);

      if (res.code) {
        this.subjects = res.data.rows;

        this.total = res.data.total;
      }
    },
    // 增加专业
    async addSubject() {
      const res = await this.$api.subjects.add(this.form);
      if (res.code) {
        this.$message({
          message: "新增专业成功",
          type: "success",
        });
        
      } else {
        this.$message.error("新增专业失败");
      }
    },
    // 删除一个专业数据
    async handleDelete(row) {
      const res = await this.$api.subjects.delete({ _id: row._id });
      if (res.code) {
        // console.log(res);
        this.$message({
          message: "删除班级成功",
          type: "success",
        });
        this.getSubjects();
      } else {
        this.$message.error("删除班级失败");
      }
    },

    //点击跳转到修改页面
    handleEdit(row) {
      this.$router.push(`/home/classUpdate/${row._id}`);
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageData.pageSize = val;
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageData.currentPage = val;
    },
  },
};
</script>

<style></style>
